Completed
Push — master ( 9899df...273a12 )
by
unknown
02:23
created

publish-all.js ➔ ... ➔ p.catch   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 4
rs 10
nop 0
1
import clc from 'cli-color'
2
import path from 'path'
3
import fse from 'fs-extra'
4
5
// ./node_modules/.bin/babel-node src/cli/process/publish-all.js ABE_WEBSITE=/path/to/website
6
// ./node_modules/.bin/babel-node src/cli/process/publish-all.js FILEPATH=/path/to/website/path/to/file.html ABE_WEBSITE=/path/to/website
7
var pConfig = {}
8
Array.prototype.forEach.call(process.argv, (item) => {
9
  if (item.indexOf('=') > -1) {
10
    var ar = item.split('=')
11
    pConfig[ar[0]] = ar[1]
12
  }
13
})
14
15
if(typeof pConfig.ABE_PATH === 'undefined' || pConfig.ABE_PATH === null) {
16
  pConfig.ABE_PATH = ''
17
}
18
19
console.log(clc.green('start publish all') + ' path ' + pConfig.ABE_PATH)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
20
21
function msToTime(duration) {
22
  var milliseconds = parseInt((duration%1000)/100)
23
        , seconds = parseInt((duration/1000)%60)
24
        , minutes = parseInt((duration/(1000*60))%60)
25
        , hours = parseInt((duration/(1000*60*60))%24)
26
27
  hours = (hours < 10) ? '0' + hours : hours
28
  minutes = (minutes < 10) ? '0' + minutes : minutes
29
  seconds = (seconds < 10) ? '0' + seconds : seconds
30
31
  return hours + ':' + minutes + ':' + seconds + '.' + milliseconds
32
}
33
34
function publishNext(published, tt, cb, i = 0) {
35
  var currentDateStart = new Date()
36
  var pub = published.shift()
37
  if(typeof pub !== 'undefined' && pub !== null) {
38
    
39
    var json = FileParser.getJson(pub.path)
40
    if(typeof json.abe_meta !== 'undefined' && json.abe_meta !== null) {
41
      i++
42
43
      var p = new Promise((resolve, reject) => {
0 ignored issues
show
Unused Code introduced by
The parameter reject is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
44
        try {
45
          
46
          cmsOperations.save.save(
47
            pub.path,
48
            json.abe_meta.template,
49
            null,
50
            '',
51
            'publish',
52
            null,
53
            'publish',
54
            true)
55
            .then(() => {
56
              var d = new Date(new Date().getTime() - currentDateStart.getTime())
57
              var total = new Date(new Date().getTime() - dateStart.getTime())
58
              // logsPub += i + ' [' + d + 'sec] > start publishing ' + pub.path .replace(config.root, '') + ' < ' + pub.path
59
              console.log(clc.green(i) + '/' + tt + ' - (' + clc.green(msToTime(d)) + '/' + msToTime(total) + ')')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
60
              console.log(clc.bgWhite(clc.black(pub.path.replace(config.root, '').replace(config.data.url, '')))
61
                + ' (' + clc.cyan(json.abe_meta.template) + ')')
62
              resolve()
63
            }).catch(function(e) {
64
              var d = new Date(new Date().getTime() - currentDateStart.getTime())
65
              var total = new Date(new Date().getTime() - dateStart.getTime())
66
              console.log(clc.red(i) + '/' + tt + ' - (' + clc.red(msToTime(d)) + '/' + msToTime(total) + ')')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
67
              publishErrors.push({
68
                msg: e + ''
69
                , json:json
70
              })
71
              console.log(clc.red(e))
72
              console.log('publish-all', 'ERROR on ' + pub.path.replace(config.root, '').replace(config.data.url, ''))
73
              resolve()
74
            })
75
        } catch(e) {
76
          console.log(clc.red('cannot save') + ' ' + pub.path)
77
          resolve()
78
        }
79
      })
80
    }
81
82
    p.then(function () {
0 ignored issues
show
Bug introduced by
The variable p does not seem to be initialized in case typeof json.abe_meta !=... json.abe_meta !== null on line 40 is false. Are you sure this can never be the case?
Loading history...
83
      publishNext(published, tt, cb, i++)
84
    })
85
    .catch(function () {
86
      publishNext(published, tt, cb, i++)
87
      console.log('error', clc.red(e))
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
Bug introduced by
The variable e seems to be never initialized.
Loading history...
88
    })
89
  }else {
90
    cb(i)
91
  }
92
}
93
94
var publishErrors = []
95
var dateStart = new Date()
96
// var logsPub = ""
97
if(typeof pConfig.ABE_WEBSITE !== 'undefined' && pConfig.ABE_WEBSITE !== null) {
98
  var config = require('../../cli').config
99
  if(pConfig.ABE_WEBSITE) config.set({root: pConfig.ABE_WEBSITE.replace(/\/$/, '') + '/'})
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
100
  try {
101
102
    // require controller to instanciate hooks
103
    var controllers = require('../../server/controllers')
104
    var FileParser = require('../../cli').FileParser
105
    var fileUtils = require('../../cli').fileUtils
106
    var folderUtils = require('../../cli').folderUtils
107
    var cmsOperations = require('../../cli').cmsOperations
108
    var Manager = require('../../cli').Manager
109
110
    Manager.instance.init()
111
      .then(() => {
112
        var i = 0
0 ignored issues
show
Unused Code introduced by
The variable i seems to be never used. Consider removing it.
Loading history...
113
114
        var files = Manager.instance.getList()
115
116
        // var result = []
117
        var published = []
118
        var folderToParse = path.join(config.root, config.data.url, pConfig.ABE_PATH)
119
        Array.prototype.forEach.call(files, (file) => {
120
          if (typeof file.publish !== 'undefined' && file.publish !== null && file.path.indexOf(folderToParse) > -1) {
121
            published.push(file)
122
          }
123
        })
124
125
        console.log('Found ' + clc.green(published.length) + ' to republish')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
126
127
        dateStart = new Date()
128
        publishNext(published, published.length, function (i) {
129
          console.log('total ' + clc.green(i) + ' files')
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
130
          if (publishErrors.length > 0) {
131
            var errorPath = path.join(config.root, 'abe-publish-all.' + dateStart.getTime() + '.log')
132
            console.log('Errors ' + clc.red(publishErrors.length) + ' see ' + errorPath + ' for more info')
133
            fse.writeJsonSync(errorPath, publishErrors, {
134
              space: 2,
135
              encoding: 'utf-8'
136
            })
137
          }
138
          dateStart = (Math.round((new Date().getTime() - dateStart.getTime()) / 1000 / 60 * 100)) / 100
139
          console.log('publish process finished in ' + clc.green(dateStart) + 'm')
140
          process.exit(0)
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
141
        })
142
      })
143
      .catch((e) => {
144
        console.log('publish-all', e)
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
145
      })
146
147
  } catch(e) {
148
    console.log('publish-all', e)
149
  }
150
151
}else {
152
  console.log('ABE_WEBSITE is not defined use node process.js ABE_WEBSITE=/pat/to/website')
153
  process.exit(0)
0 ignored issues
show
Compatibility Debugging Code Best Practice introduced by
Use of process.exit() is discouraged as it will potentially stop the complete node.js application. Consider quitting gracefully instead by throwing an Error.
Loading history...
154
}